layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 公司信息表管理
     */
    var Company = {
        tableId: "companyAuthTable"
    };

    /**
     * 初始化表格的列：认证状态：0-默认，1-待审核，2-审核失败，3-审核通过
     */
    Company.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', align: 'center', hide: true, title: ''},
            {field: 'logo', align: 'center', sort: true, title: '公司logo', templet: '#pic' },
            {field: 'name',align: 'center',  sort: true, title: '公司简称'},
            {field: 'realName',align: 'center',  sort: true, title: '企业名称'},
            {field: 'legalPerson',align: 'center',  sort: true, title: '法人姓名'},
            {field: 'identity',align: 'center',  sort: true, title: '营业执照号'},
            {field: 'businessLicense',align: 'center',  sort: true, title: '营业执照', templet: '#pic2' },
            {field: 'verifyStatus', align: 'center', sort: true, title: '审核状态', templet: function (d) {
                    var flag_box = "";
                    if (d.verifyStatus === 1) {
                        flag_box = "<span class='layui-btn layui-btn-xs layui-btn-danger' >待审核</span>";
                    } else if (d.verifyStatus === 2) {
                        flag_box = "<span class=\"layui-btn layui-btn-xs layui-btn-disabled \">审核失败</span>";
                    } else if (d.verifyStatus === 3) {
                        flag_box = "<span class=\"layui-btn layui-btn-xs \">审核通过</span>";
                    }
                    return flag_box;
                }},
            {field: 'refuseReason', align: 'center', sort: true, title: '失败原因'},
            {field: 'addAuthTime', align: 'center', sort: true, title: '提交时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Company.search = function () {
        var queryData = {};
        queryData['keyword'] = $("#condition").val();
        table.reload(Company.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    Company.openAddDlg = function () {
        func.open({
            title: '添加公司信息表',
            content: Feng.ctxPath + '/company/add',
            tableId: Company.tableId
        });
    };

    /**
    * 通过
    *
    * @param data 点击按钮时候的行数据
    */
    Company.openEditDlg = function (data) {
        var operation = function () {
            data.verifyStatus = 3;
            var ajax = new $ax(Feng.ctxPath + "/company/companyAuth", function (data) {
                Feng.success("操作成功！");
                table.reload(Company.tableId);
            }, function (data) {
                Feng.error("更新失败！" + data.responseJSON.message)
            });
            ajax.set(data);
            ajax.start();
        };
        Feng.confirm("是否通过?", operation);
    };

    /**
     * 拒绝
     *
     * @param data 点击按钮时候的行数据
     */
    Company.onDeleteItem = function (data) {
        layer.prompt({title: '请填写拒绝原因', formType: 2}, function (text, index) {
            data.refuseReason = text;
            data.verifyStatus = 2;
            layer.close(index);
            var ajax = new $ax(Feng.ctxPath + "/company/companyAuth", function (data) {
                Feng.success("操作成功！");
                table.reload(Company.tableId);
            }, function (data) {
                Feng.error("更新失败！" + data.responseJSON.message)
            });
            ajax.set(data);
            ajax.start();
        });
    };

    /**
     * 导出excel按钮
     */
    Company.exportExcel = function () {
        var checkRows = table.checkStatus(Company.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Company.tableId,
        url: Feng.ctxPath + '/company/authList',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Company.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Company.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Company.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Company.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Company.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Company.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Company.onDeleteItem(data);
        }else if (layEvent === 'open_image') {
            Company.openImage(data.logo);
        }else if (layEvent === 'open_image2') {
            Company.openImage(data.businessLicense);
        }
    });

    /*
  *点击图片
   */
    Company.openImage=function (data) {
        layer.open({
            type: 1,
            title: false,
            closeBtn: 1,
            area: ['auto'],
            skin: 'layui-layer-nobg', //没有背景色
            shadeClose: true,
            content: "<div class='hide' ><img src='"+data+"' style='max-height: 600px'></div> "
        });
    };
});
